package com.zs.servlet;

import com.zs.http.Request;
import com.zs.http.Response;

import java.io.IOException;
import java.io.OutputStream;

public class MyImplServlet extends MyHttpServlet{

    public MyImplServlet() {
        init();
    }

    @Override
    public void doGet(Request request, Response response) {
        try {
            System.out.println("get请求");
            Thread.sleep(200);
            //省略业务调用的代码，tomcat会根据request对象里面的inputStream拿到对应的参数进行业务调用
            System.out.println("服务器进行业务处理");
            //模拟业务层调用后的返回
            OutputStream outputStream=response.getOutputStream();
            String result=Response.responsebody+"user handle successful";
            outputStream.write(result.getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(Request request, Response response) {
        try {
            System.out.println("post请求");
            //省略业务调用的代码，tomcat会根据request对象里面的inputStream拿到对应的参数进行业务调用
            System.out.println("服务器进行业务处理");
            //模拟业务层调用后的返回
            OutputStream outputStream=response.getOutputStream();
            String result=Response.responsebody+"user handle successful";
            outputStream.write(result.getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //初始化考虑在构造方法中调用
    public void init() {
        System.out.println("servlet 初始化");
    }

    //个人觉得可以参考对象回收时的finalize方法
    public void destroy() {
        System.out.println("servlet 销毁");
    }
}
